<template>
	<view class="content padding">
		<block class="">
			<view class="cuIcon-title margin-bottom">
				<text class="text-df ">本套餐包包含以下优惠券</text>
			</view>
			<block v-for="(item, index) in couponsInfo" :key="index">
				<view
					class="coupon bg-img flex flex-direction "
					@click="jump('/pages/coupon/couponDetail?couponId=' + item.id)"
				>
					<view class="flex align-center " style="height: 240rpx;">
						<image
							:src="item.image"
							mode="aspectFill"
							class="radius  margin-left"
							style="width: 160rpx; height: 160rpx;"
						></image>
						<view class="flex flex-direction margin-left">
							<text class="text-df">{{ item.name }}</text>
							<text class="text-price text-bold" style="color: #F8CE39; font-size: 70rpx;">
								{{ item.price }}
							</text>
						</view>
					</view>
					<view class="flex align-center padding-left">
						<text style="color: #666666;">{{ item.usageTime }}</text>
					</view>
					<view class="immediately flex flex-direction align-center justify-center">
						<text>立</text>
						<text>即</text>
						<text>查</text>
						<text>看</text>
					</view>
				</view>
			</block>
			<button class="cu-btn determine" @click="buyPackage()">购买优惠券包 , 获得全部优惠券</button>
		</block>
		<!-- <image src="https://sprtcar.oss-cn-chengdu.aliyuncs.com/home/dataIsEmpty.png" mode="aspectFill" class="emptyIcon " v-else></image> -->
		<u-toast ref="uToast" />
	</view>
</template>

<script>
const app = getApp();
export default {
	data() {
		return {
			couponsInfo: [],
			couponPackageId: null,
			price: ''
		};
	},
	onShow() {},
	onLoad(options) {
		console.log(' options:>> ', options);
		this.couponPackageId = options.couponPackageId;
		this.price = options.price;

		var that = this;
		app.globalData.api.getCouponByPackageId(options.couponPackageId).then(res => {
			that.couponsInfo = res.resultObj;
			if (res.resultObj.length == 0) {
				this.$refs.uToast.show({ title: '暂未优惠券包~~', type: 'error' });
			}
		});
	},
	methods: {
		jump(url) {
			uni.navigateTo({
				url
			});
		},
		buyPackage() {
			/* app.globalData.api.buyPackage(this.couponPackageId, app.globalData.smmUser.id).then(res => {
				if (res.resultObj > 0) {
					uni.showModal({
						title: '购买成功',
					});
				}
			}); */

			var that = this;
			app.globalData.api.wxPay(this.price).then(res => {
				wx.requestPayment({
					timeStamp: res.resultObj.wxPayRequest.timeStamp,
					nonceStr: res.resultObj.wxPayRequest.nonceStr,
					package: res.resultObj.wxPayRequest.packageValue,
					signType: 'MD5',
					paySign: res.resultObj.wxPayRequest.paySign,
					success(res1) {
						console.log(" app.globalData---------------: ", app.globalData);
						that.$refs.uToast.show({ title: '购买优惠券成功~~', type: 'success' });

						app.globalData.api
							.buyPackage(that.couponPackageId, app.globalData.smmUser.id)
							.then(res => {
								console.log("res--------------: ",res);
								if (res.resultObj > 0) {
									setTimeout(() => {
										uni.navigateTo({
											url: '../user-center/coupon'
										});
									}, 1000);
								}
							});
					},
					fail(res2) {
						console.log('failed', res2);
					}
				});
			});
		}
	}
};
</script>

<style lang="scss" scoped>
.content {
	.coupon {
		position: relative;
		width: 690rpx;
		height: 290rpx;
		margin: 0 auto;
		margin-bottom: 20rpx;
		background-image: url(https://sprtcar.oss-cn-chengdu.aliyuncs.com/home/coupon.png);
		.immediately {
			position: absolute;
			top: 0;
			right: 0;
			//transform: translateY(-50%);
			width: 125rpx;
			height: 240rpx;
			font-size: 28rpx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #666666;
		}
	}
	.determine {
		width: 690rpx;
		height: 80rpx;
		background: #f8ce39;
		border-radius: 12rpx;

		font-size: 30rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: #333333;
		line-height: 80rpx;
		margin: 60rpx 0;
	}
}
</style>
